<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" placeholder="请输入备注"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="员工编号" prop="employeeNumber">
            <el-input v-model="form.employeeNumber" placeholder="请输入员工编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="用户 ID" prop="user">
            <el-input v-model="form.user" placeholder="请输入用户 ID"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="用户姓名" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入用户姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="身份证号" prop="idNumber">
            <el-input v-model="form.idNumber" placeholder="请输入身份证号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="身份证号是否认证通过" prop="idNumber1">
            <el-input v-model="form.idNumber1" placeholder="请输入身份证号是否认证通过"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="人员类型" prop="personnelType">
            <el-input v-model="form.personnelType" placeholder="请输入人员类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="人员状态" prop="personnelStatus">
            <el-input v-model="form.personnelStatus" placeholder="请输入人员状态"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="手机号" prop="cellphoneNumber">
            <el-input v-model="form.cellphoneNumber" placeholder="请输入手机号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="办公室电话" prop="officePhone">
            <el-input v-model="form.officePhone" placeholder="请输入办公室电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="备用手机号" prop="backupPhoneNumber">
            <el-input v-model="form.backupPhoneNumber" placeholder="请输入备用手机号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="性别" prop="gender">
            <el-input v-model="form.gender" placeholder="请输入性别"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="生日" prop="birthday">
            <el-input v-model="form.birthday" placeholder="请输入生日"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="头像地址" prop="avatarAddress">
            <el-input v-model="form.avatarAddress" placeholder="请输入头像地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="学历" prop="educationalBackground">
            <el-input v-model="form.educationalBackground" placeholder="请输入学历"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="职称" prop="title">
            <el-input v-model="form.title" placeholder="请输入职称"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="公务员职务" prop="civilServantPosition">
            <el-input v-model="form.civilServantPosition" placeholder="请输入公务员职务"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="公务员职级" prop="civilServantRank">
            <el-input v-model="form.civilServantRank" placeholder="请输入公务员职级"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="事业单位岗位" prop="sydwgw">
            <el-input v-model="form.sydwgw" placeholder="请输入事业单位岗位"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="事业单位岗位等级" prop="sydwgwdj">
            <el-input v-model="form.sydwgwdj" placeholder="请输入事业单位岗位等级"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="事业单位岗位职级" prop="sydwgwjb">
            <el-input v-model="form.sydwgwjb" placeholder="请输入事业单位岗位职级"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="是否政协委员" prop="sfzxwy">
            <el-input v-model="form.sfzxwy" placeholder="请输入是否政协委员"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="是否人大代表" prop="sfrddb">
            <el-input v-model="form.sfrddb" placeholder="请输入是否人大代表"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="民族" prop="nationality">
            <el-input v-model="form.nationality" placeholder="请输入民族"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="党派" prop="party">
            <el-input v-model="form.party" placeholder="请输入党派"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="是否禁用" prop="doYouWantToDisableIt">
            <el-input v-model="form.doYouWantToDisableIt" placeholder="请输入是否禁用"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="是否虚拟" prop="isItVirtual">
            <el-input v-model="form.isItVirtual" placeholder="请输入是否虚拟"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="SystemUserExtensionInformationGuizhengtongDialog">
// ========== 1. 导入语句 ==========
import { useDict } from '/@/hooks/dict';
import { rule } from '/@/utils/validate';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/forestry/systemUserExtensionInformationGuizhengtong';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  remarks: '', // 备注
  employeeNumber: '', // 员工编号
  user: '', // 用户 ID
  userName: '', // 用户姓名
  idNumber: '', // 身份证号
  idNumber1: '', // 身份证号是否认证通过
  personnelType: '', // 人员类型
  personnelStatus: '', // 人员状态
  cellphoneNumber: '', // 手机号
  officePhone: '', // 办公室电话
  backupPhoneNumber: '', // 备用手机号
  gender: '', // 性别
  birthday: '', // 生日
  address: '', // 地址
  avatarAddress: '', // 头像地址
  educationalBackground: '', // 学历
  title: '', // 职称
  civilServantPosition: '', // 公务员职务
  civilServantRank: '', // 公务员职级
  sydwgw: '', // 事业单位岗位
  sydwgwdj: '', // 事业单位岗位等级
  sydwgwjb: '', // 事业单位岗位职级
  sfzxwy: '', // 是否政协委员
  sfrddb: '', // 是否人大代表
  nationality: '', // 民族
  party: '', // 党派
  doYouWantToDisableIt: '', // 是否禁用
  isItVirtual: '', // 是否虚拟
});

// ========== 4. 字典数据处理 ==========

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  employeeNumber: [
    { required: true, message: '员工编号不能为空', trigger: 'blur' }
  ],
  userName: [
    { required: true, message: '用户姓名不能为空', trigger: 'blur' }
  ],
  idNumber: [
    { required: true, message: '身份证号不能为空', trigger: 'blur' }
  ],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getSystemUserExtensionInformationGuizhengtongData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取SystemUserExtensionInformationGuizhengtong信息
  if (id) {
    form.id = id;
    getSystemUserExtensionInformationGuizhengtongData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交
  
  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script> 